//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Collections;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using CommunityServer.Components;
using CommunityServer.Controls;
using CommunityServer.Galleries.Components;

namespace CommunityServer.Galleries.Controls
{
	/// <summary>
	/// Summary description for TagBreadCrumb.
	/// </summary>
	public class TagBreadCrumb : CommunityServer.Controls.TagBreadCrumb
	{
		protected override void CreateChildControls() 
		{
			if (this.ShowHome)
			{
				this.ShowHome = false;
				crumbs.Enqueue(GetAnchor(ResourceManager.GetString("home"), GalleryUrls.Instance().ViewGallery(this.CurrentGallery.ApplicationKey)));
			}

			base.CreateChildControls();
		}

		protected override string GetTagUrl()
		{
			return GalleryUrls.Instance().TagsHome(this.CurrentGallery.ApplicationKey);
		}

		protected override string GetTagUrl(string[] tagsPath)
		{
			return GalleryUrls.Instance().TagsBrowser(this.CurrentGallery.ApplicationKey, tagsPath);
		}

		protected override string GetTagRssUrl(string[] tagsPath)
		{
			if (this.CurrentGallery.EnableRss)
			{
				CSContext context = CSContext.Current;
				bool isAvailableToPublic = Permissions.ValidatePermissions(CurrentGallery, Permission.View, Users.GetAnonymousUser(true));

				if (isAvailableToPublic)
					return GalleryUrls.Instance().Rss(this.CurrentGallery.ApplicationKey, tagsPath, true);
				else if (context.SiteSettings.EnableSecureSyndication)
					return SiteUrls.FormatUrlWithUserNameToken(GalleryUrls.Instance().Rss(this.CurrentGallery.ApplicationKey, tagsPath, true), context.User, true);

			}

			return null;
		}


		#region Gallery Properties

		private Gallery gallery = null;

		/// <summary>
		/// Returns the CurrentWeb and calls Authorize
		/// </summary>
		public virtual Gallery CurrentGallery
		{
			get
			{
				if(gallery == null)
				{
					string appKey = CSContext.Current.ApplicationKey;
					if(Globals.IsNullorEmpty(appKey))
						appKey = GalleryConfiguration.Instance().DefautApplicationKey;

					gallery = Galleries.GetGallery(appKey);

					if(!CurrentUser.IsGalleryAdministrator)
					{
						Authorize(gallery);
					}

				}

				return gallery;
			}
		}

		protected User CurrentUser
		{
			get{return csContext.User;}
		} 

		protected virtual void Authorize(Gallery g)
		{
			if(g == null)
				throw new CSException(CSExceptionType.SectionNotFound, string.Format("Gallery {0} could not be found", CSContext.Current.ApplicationKey));


			if(Context.Items[CurrentUser.Username + "Permission:" + Permission.View.ToString()] == null)
			{
				Permissions.AccessCheck(g,Permission.View,this.CurrentUser);
				Context.Items[CurrentUser.Username + "Permission:" + Permission.View.ToString()] = true;

			}
		}

		#endregion
	}
}
